﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QMessageBox>
#include <QSettings>  //ini文件读写
#include <QFile>
#include <QKeyEvent>
#include <QAction>
#include <QCursor>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QCloseEvent>
#include <QToolTip>
#include <QInputDialog>
#include <QListView>
//串口头文件，另外还需要在Pro文件中添加QT += serialport
#include <QTime>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QTimer>
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
#include <QRect>
#include <QMovie>
#include <QSqlTableModel>

#include "protocol.h"
#include "protocol_07645.h"
#include "comfunc.h"
#include "sql_operator.h"
#include "dlg_selectdid.h"
#include "dlg_select_7e_did.h"
#include "dlg_preserve_remote_cmd.h"
#include "dlg_preserve_local_cmd.h"
#include "dlg_preserve_645_func_code.h"
#include "dlg_common_frame.h"
#include "dlg_setting.h"
#include "smart_plc.h"
#include "frameboxbody.h"
#include "qlineeditex.h"

extern QSerialPort myserial;
extern QSqlTableModel_My *model_645_did;
extern QSqlTableModel_My *model_7E_did;
extern QSqlTableModel_My *model_7E_RemoteCMD;
extern QSqlTableModel_My *model_7E_LocalCMD;
extern QSqlTableModel_My *model_645_FuncCode;
extern QSqlTableModel_My *model_common_7E;


struct Settings
{
    uint8 key_showDidAnaly;
};

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void contextMenuEvent(QContextMenuEvent *);//右键菜单
protected:
    bool eventFilter(QObject *o, QEvent *e);//重写事件过滤器，用installEventFilter安装后生效
    void closeEvent(QCloseEvent *event);//重写窗口的关闭事件
    void keyPressEvent(QKeyEvent *event);//重写键盘按下事件
    void keyReleaseEvent(QKeyEvent *event);//重写键盘弹起事件
    void resizeEvent(QResizeEvent *event);//重写尺寸变化事件
    void focusInEvent(QFocusEvent *event);
//    void dragLeaveEvent(QDragLeaveEvent *event);
private slots:

    void on_serial_receive_data();//串口接收服务
    void on_timer_serial_rece_timeout();//串口接收超时服务
    void on_SerialPortError_occurred(QSerialPort::SerialPortError error);//串口抛出错误_处理       
    void on_timer_recycle_send_timeout();//循环发送服务
    void on_DidSelect(QString);//从其他窗口接收645 did
    void on_ReceSettings(struct Settings);//接收其他窗口的设置信息
    void on_Act_settings_triggered();//右键菜单
    void on_Act_about_triggered();//右键菜单
    void on_7E_DidSelect(QString);//从其他窗口接收7e did
    void on_loacl_frame_encoded(const char *data, qint64 len);//从其他窗口收到组建好的本地报文
    void on_pushButtonScan_clicked();
    void on_pushButtonOpenSerial_clicked();
    void on_pushButtonSend_clicked();
    void on_pushButton_clicked();
    void on_pushButtonClrTable_clicked();
    void on_pushButtonSelectDid_clicked();    
    void on_lineEditDID_editingFinished();
    void on_lineEditTaid_textChanged(const QString &arg1);
    void on_comboBoxCMD645_currentIndexChanged(const QString &arg1);
    void on_checkBoxBroadcast_stateChanged(int arg1);
    void on_tableWidgetDisp_clicked(const QModelIndex &index);
    void on_pushButtonSetSaid_clicked();
    void on_pushButtonSetPanid_clicked();
    void on_pushButtonSelect7EDid_clicked();
    void on_checkBoxUse645_stateChanged(int arg1);
    void on_pushButtonPreserveRemoteCMD_clicked();
    void on_checkBoxCustomFrame_stateChanged(int arg1);
    void on_pushButtonSend645_clicked();
    void on_pushButton_2_clicked();
    void on_spinBoxFrameInterval_editingFinished();
    void on_pushButtonFuncCode_clicked();
    void on_pushButtonSetbaud_clicked();
    void on_pushButtonGetPlcVer_clicked();
    void on_checkBoxRecycle_stateChanged(int arg1);
    void on_checkBoxFoldLocal_stateChanged(int arg1);
    void on_pushButtonAddToTest_clicked();
    void on_pushButtonPreserveTest_clicked();
    void on_comboBoxTestExample_currentIndexChanged(const QString &arg1);
    void on_checkBoxFoldSerial_stateChanged(int arg1);
    void on_spinBoxMultiDid_valueChanged(int arg1);
    void on_lineEditDID_customContextMenuRequested(const QPoint &pos);
    void on_pushButtonAppendSum_clicked();
    void on_pushButtonClearCustomFrame_clicked();
    void on_SliderSetWidth_valueChanged(int value);
    void on_SliderSetWidth_sliderReleased();

private:
    Ui::MainWindow *ui;
    QMenu *menu;
    QAction* Act_about;
    QAction* Act_settings;
    FrameBoxBody *frameBoxBody[4];
    QLabel *labelPush;
    struct Settings settings={0};
    void serial_ui_init(void);

    int encodeFrameBody(struct FBD_body *frame_body);
    struct SHS_frame  *encodeFrame7E_FromGui();
    struct frame07645 *encodeFrame645_FromGui();
    void insert_frame_to_table(QString send_or_rece,QByteArray byte);
    void my_serial_send(const char *data, qint64 len);
    void sql_load_and_init();
    void ini_save_or_restore(bool isSave);
    void init_animation();



};

#endif // MAINWINDOW_H
